package com.jrx.anytxn.transaction.job.paph.config;

import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.transaction.config.PaphFileConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.job.paph.step.MergeBankFileListener;
import com.jrx.anytxn.transaction.service.IPaphFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author liuyu
 * @Description 合并分库文件批量
 */
@Configuration
public class MergeBankFileConfiguration {
	private static final Logger logger = LoggerFactory.getLogger(MergeBankFileConfiguration.class);

	@Autowired
	private JobBuilderFactory jobBuilderFactory;
	@Autowired
	private StepBuilderFactory stepBuilderFactory;
	@Autowired
	private MergeBankFileListener mergeBankFileListener;
	@Autowired
	private PaphFileConfig paphFileConfig;
	@Autowired
	private IPaphFileService paphFileService;

	@Bean
	public Job mergeBankFileJob() {
		return jobBuilderFactory.get("mergeBankFileJob")
				.listener(mergeBankFileListener)
				.start(mergeBankFileStep1())
				.next(mergeBankFileStep2())
				.next(mergeBankFileStep3())
				.build();
	}


	@Bean
	public Step mergeBankFileStep1() {
		return stepBuilderFactory.get("mergeBankFileStep1")
				.tasklet(mergeBankFileStep1Tasklet())
				.build();
	}

	@Bean
	@StepScope
	public Tasklet mergeBankFileStep1Tasklet() {
		return (contribution, chunkContext) -> {
			logger.debug("合并生成放款转账文件和md5文件开始。。");
			paphFileService.mergeBankFile(paphFileConfig.getTraAcctDetailFilePath(), TransBizConstant.TRA_ACCT_DETAIL
					, Constant.PRODECUT_TYPE_JY);
			paphFileService.mergeBankFile(paphFileConfig.getTraAcctDetailFilePath(), TransBizConstant.TRA_ACCT_DETAIL
					, Constant.PRODECUT_TYPE_XF);

			logger.debug("合并生成放款转账文件和md5文件结束。。");
			return RepeatStatus.FINISHED;
		};
	}

	@Bean
	public Step mergeBankFileStep2() {
		return stepBuilderFactory.get("mergeBankFileStep2")
				.tasklet(mergeBankFileStep2Tasklet())
				.build();
	}

	@Bean
	@StepScope
	public Tasklet mergeBankFileStep2Tasklet() {
		return (contribution, chunkContext) -> {
			logger.debug("合并生成还款计划文件和md5文件开始。。");
			paphFileService.mergeBankFile(paphFileConfig.getPlanFilePath(), TransBizConstant.PLAN_FILE_NAME,
					Constant.PRODECUT_TYPE_JY);
			paphFileService.mergeBankFile(paphFileConfig.getPlanFilePath(), TransBizConstant.PLAN_FILE_NAME,
					Constant.PRODECUT_TYPE_XF);
			logger.debug("合并生成还款计划文件和md5文件结束。。");
			return RepeatStatus.FINISHED;
		};
	}

	@Bean
	public Step mergeBankFileStep3() {
		return stepBuilderFactory.get("mergeBankFileStep3")
				.tasklet(mergeBankFileStep3Tasklet())
				.build();
	}

	@Bean
	@StepScope
	public Tasklet mergeBankFileStep3Tasklet() {
		return (contribution, chunkContext) -> {
			logger.debug("合并生成扣款结果文件和md5文件开始。。");
			paphFileService.mergeBankFile(paphFileConfig.getRepayDetailFilePath(), TransBizConstant.PREPAY_DETAIL,
					Constant.PRODECUT_TYPE_JY);
			paphFileService.mergeBankFile(paphFileConfig.getRepayDetailFilePath(), TransBizConstant.PREPAY_DETAIL,
					Constant.PRODECUT_TYPE_XF);

			logger.debug("合并生成扣款结果文件和md5文件结束。。");
			return RepeatStatus.FINISHED;
		};
	}
}
